import Toast from '@vant/weapp/toast/toast';
const nav_img_black = 'https://witty6.oss-cn-beijing.aliyuncs.com/demo/64241692603437_.pic.jpg'
Page({
  data: {
    nav_show:true,
    nav_title:'秉华健康-健康数据管家',
    fontColor:'#333333',
    nav_img:nav_img_black,
    // 表单信息
    form:{
      name:'',
      sex:'',
      birthday:'',
      tel:'',
      height:'',
      weight:'',
      work:'',
      nation:'',
      location:'',
      place:'',
    },
    // 表单验证信息
    formrules:{
      nameErr:'',
      sexErr:'',
      birthdayErr:'',
      telErr:'',
    },
    // 控制单选picker弹出
    showPopup:false,
    // 控制日期picker弹出
    showdata:false,
    //控制日期picker父传子参数
    pickertype:'',
    //控制地点picker父传子参数
    locationtype:'',
    // 控制地点picker弹出
    showlocation:false,
    // 单一picker假数据
    columns:[],
    columns1:["性别"],
    columns2:["身高"],
    columns3:["体重"],
    columns4:["职业"],
    columns5:["民族"],
    checked: true,
  },

  // 输入框方法
  onChange(event) {
    // 用switch控制信息变化,方法名为data-input,绑定于id_infor_list.wxml
    switch(event.currentTarget.dataset['input']) {
        case 'name':
          let name = event.detail
            this.setData({
            "form.name":name,
          })
          break;
        case 'tel':
          let tel = event.detail
          this.setData({
          "form.tel":tel,
          })
          break;
    }
  },
  // 控制向picker组件传递的值,方法名为data-picker,绑定于id_infor_list.wxml
  showPopup(event){
    // 获取van-field内参数
    let pickertype = event.currentTarget.dataset['picker']
    this.setData({
      showPopup:true,
      pickertype:pickertype,
      columns:this.data.columns1
    })
    // 控制数组假数据
    switch(pickertype) {
      case 'sex':
        this.setData({
          columns:this.data.columns1
        })
      break;
      case 'height':
        this.setData({
          columns:this.data.columns2
        })
      break;
      case 'weight':
        this.setData({
          columns:this.data.columns3
        })
      break;
      case 'work':
        this.setData({
          columns:this.data.columns4
        })
      break;
      case 'nation':
        this.setData({
          columns:this.data.columns5
        })
      break;
    }
  },
  // 接受子组件传递的值用pickertype判断信息的改变
picker(event) {
  switch(event.detail.pickertype) {
    case 'sex':
      let sex = event.detail.pickerdetail
      this.setData({ "form.sex":sex,})
    break;
    case 'height':
      let height = event.detail.pickerdetail
      this.setData({"form.height":height,})
    break;
    case 'weight':
      let weight = event.detail.pickerdetail
      this.setData({"form.weight":weight,})
    break;
    case 'work':
      let work = event.detail.pickerdetail
      this.setData({"form.work":work,})
    break;
    case 'nation':
      let nation = event.detail.pickerdetail
      this.setData({"form.nation":nation,})
    break;
  }
},
  //  获取子组件传值
  pickerdata(event){
    let pickerbirthday = event.detail.birthday
    // const { birthday } =this.data.form.birthday
    this.setData({
      "form.birthday":pickerbirthday
    })
  },
  // 用于展示日期选择器
  showdata(){
    this.setData({ showdata: true });
  },

  // 控制地点picker显示隐藏，并向子组件传送locationtype
  showlocation(event){
    let locationtype = event.currentTarget.dataset['location']
    console.log(locationtype)
    this.setData({ 
      showlocation: true,
      locationtype:locationtype
    });
  },
  // 接受子组件locationtype，并赋值
  pickerlocation(event){
    switch(event.detail.locationtype) {
      case 'location':
        let location = event.detail.locationname
        this.setData({ "form.location":location,})
      break;
      case 'place':
        let place = event.detail.locationname
        this.setData({ "form.place":place,})
        console.log("22222222")
      break;
    }
  },

  // 复选框方法--开始
  onChangechecked(event) {
    this.setData({
      checked: event.detail,
    });
  },
  // 复选框方法--结束

  To_agree_policy(e){
    console.log("cool")
    console.log(e.currentTarget.dataset.navigatename)
    let navigatename = e.currentTarget.dataset.navigatename
    console.log(navigatename)
    wx.navigateTo({ url: '../agree_policy/agree_policy?name='+ navigatename})
  },

  // 提交按钮--引入checkFn方法做表单验证
  submit_btn(){
    this.checkFn('name','nameErr','昵称不能为空')
    this.checkFn('sex','sexErr','性别不能为空')
    this.checkFn('birthday','birthdayErr','出生日期不能为空')
    this.checkFn('tel','telErr','手机号不能为空')

    if(this.data.form.name.trim()&&this.data.form.sex.trim()&&this.data.form.birthday.trim()&&this.data.form.tel.trim()&&this.data.checked){
      Toast('跳转');
      console.log("1111111111111")
    }else{
      Toast('请输入完整信息');
      console.log("2222222222")
    }
  },
  // checkFn方法表单验证
  checkFn(err,errKey,errStr){
    if(!this.data.form[err].trim()){
        this.setData({[errKey]:errStr})
        return false
      }else{
        this.setData({ [errKey]:''})
        return true
      }
  },
});